//
//  SEGOrderCancelViewController.m
//  Commom
//
//  Created by Clavis on 2020/4/23.
//

#import "SEGOrderCancelViewController.h"
#import "SEGOrderDetailManager.h"
#import "UITextView+Limit.h"

@interface SEGOrderCancelViewController ()

@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UILabel *placeHolderLabel;
@property (nonatomic, strong) UILabel *tipLabel;

@end

@implementation SEGOrderCancelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.title = @"撤销";
    self.view.backgroundColor = kColor_bg;

    
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = kColor_W;
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(150);
    }];
    
    self.tipLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.text = @"250字以内";
        label.textColor = kColor_C;
        label.font = kFont_13;
        [backView addSubview:label];
        label;
    });
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-15);
    }];
    
    self.textView = [[UITextView alloc] init];
    self.textView.backgroundColor = kColor_W;
    self.textView.textColor = kColor_1;
    self.textView.font = kFont_16;
    self.textView.contentInset = UIEdgeInsetsMake(-7,0,0,0);
    self.textView.limitInputMaxLength = 250;
    [backView addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.left.mas_equalTo(9);
        make.right.mas_equalTo(-15);
        make.bottom.equalTo(self.tipLabel.mas_top).offset(-10);
    }];
    
    
    self.placeHolderLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.text = @"撤单理由";
        label.textColor = kColor_C;
        label.font = kFont_16;
        [backView addSubview:label];
        label;
    });
    [self.placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(15);
    }];
    
    UIButton *submitButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"提交" forState:UIControlStateNormal];
        [button setTitleColor:kColor_W forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(submitButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = kFont_16;
        [self.view addSubview:button];
        button;
    });
    [submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(44 + LYSafeAreaBottom());
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextChangeNotification:) name:UITextViewTextDidChangeNotification object:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - UI Handle
- (void)handleTextChangeNotification:(NSNotification *)notification {
    self.placeHolderLabel.hidden = self.textView.text.length > 0;
}

#pragma mark - Action
- (void)submitButtonTap:(UIButton *)sender {
    // 输入内容去空格判空
    if (!self.textView.text.length || ![self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length) {
        [SEGAlertView showTmepMessageWithMessage:@"请填写撤单理由"];
        return;
    }
    SEGNewAlertView *alertView = [[SEGNewAlertView alloc] init];
    [alertView showWithAlertTitle:@"提示" text:@"撤销后不可恢复，确定要撤销？" buttonTitlesArray:@[@"取消",@"确定"] actionBlock:^(NSInteger btnIndex) {
        if (btnIndex == 1) {
            [self requestCancleOrder];
        }
    }];

}

- (void)requestCancleOrder {
    NSDictionary *params = @{
        @"organId": ValueToString(self.organID),
        @"serviceOrderId" : ValueToString(self.serviceOrderID),
        @"cause" : self.textView.text
    };
    [SVProgressHUD show];
    [[SEGOrderDetailManager sharedManager] requestCancelOrder:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        [SVProgressHUD dismiss];
        if (isSuccessed) {
            [SEGAlertView showTmepMessageWithMessage:@"撤单成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTI_SEGORDERMYSERVICE_LIST_REFRESH object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [SEGAlertView showTmepMessageWithMessage:errorMessage];
        }
    }];
}

@end
